/**
 * 本类为管理订单的DAO层
 * 本类只有显示和新增的方法。
 */
package com.webshop.DataDao;

import com.webshop.JavaBean.Detail_order;
import com.webshop.JavaBean.Order;
import com.webshop.service.UltimateServie;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());

    //开了个头，这个订单需要重做！冗余太多！

    /**
     * 新增主订单
     */
    public void insertOrder(Order order) {
        String sql = "insert into `order` (orderID,userName,creattime,addressee,address,phone,totalprice,state) values(?,?,?,?,?,?,?,?)";

        Object[] parm = {order.getOrderID(), order.getUserName(), order.getCreattime(), order.getAddressee(), order.getAddress()
                , order.getPhone(), order.getTotalprice(), order.getState()};
        try {
            qr.update(sql, parm);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 新增详细订单
     * @param detail_order
     */
    public void addDetailOrder(Detail_order detail_order) {
        String sql = "insert into detail_order (orderID,goodsName,goodsAttribute,onlyPrice,goodsNum,sumPrice,picpath) values(?,?,?,?,?,?,?)";
        Object[] parm = {detail_order.getOrderID(),
                detail_order.getGoodsName(),
                detail_order.getGoodsAttribute(),
                detail_order.getOnlyPrice(),
                detail_order.getGoodsNum(),
                detail_order.getSumPrice(),
                detail_order.getPicpath()
                };
        try {
            qr.update(sql, parm);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 显式订单
     * @param name
     * @return
     */
    public List<Order> showOrder(String name)
    {
        String sql = "select * from  `order` where userName=?";
        List<Order> temp = null;
        try {
            temp = qr.query(sql, new BeanListHandler<>(Order.class), name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return temp;
    }

    /**
     * 返回子订单
     * @param orderID
     * @return
     */
    public List<Detail_order> showDetail(String orderID) {
        String sql = "SELECT * FROM `detail_order` WHERE orderID = ?";
        List<Detail_order> temp = null;
        try {
            temp = qr.query(sql, new BeanListHandler<>(Detail_order.class), orderID);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return temp;

    }

    /**
     * 已完成订单
     * @param name
     * @return
     * @throws Exception
     */
    public List<Order> finsh(String name)throws Exception{
        String sql="SELECT * FROM `order` WHERE userName= ?"+" and state =1";
        List<Order> orders=new ArrayList<>();
        orders=qr.query(sql,new BeanListHandler<>(Order.class),name);
        return  orders;
    }

    /**
     * 未完成订单
     * @param name
     * @return
     * @throws Exception
     */
    public List<Order> unfinsh(String name)throws Exception{
        String sql="SELECT * FROM `order` WHERE userName= ?"+" and state =0";
        List<Order> orders=new ArrayList<>();
        orders=qr.query(sql,new BeanListHandler<>(Order.class),name);
        return  orders;
    }

    /**
     * 修改订单为支付状态
     * @param ID
     */
    public  void update(String ID){
        String sql="UPDATE `order` set state=1 where orderID = ?";
        try {
            qr.update(sql,ID);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("flase");
        }

    }
}
